import Controller from '../../decorators/controller'
import post from '../../decorators/post'
import BaseController from '../base'
import UserDao from '../../dao/userDao'
import validate from '../../decorators/validate.js'
import {decodeAndCreateToken} from "../../util/token";

@Controller('/oauth')
export default class extends BaseController {
    @validate(require("./validateIndex").default)
    @post("/token")
    async token() {
        /*
        * 1,find user
        * 2.create token by user
        * */

        var body = this.getBody()
        var result = await UserDao.findOneByNameAndPassword({name: body.name || body.username, password: body.password})
        if (result) {
            var token = decodeAndCreateToken(result)
            this.ctx.body = {access_token: token}
        } else {
            this.returnFail("账号密码错误")
        }
        // this.ctx.body = "hello world"

        // this.ctx.body = {
        //     access_token: "aaa"
        // }
    }

}
